package org.light.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Dropdown;
import org.light.domain.Field;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Type;
import org.light.domain.Var;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.generator.NamedStatementGenerator;
import org.light.generator.NamedStatementListGenerator;
import org.light.utils.InterVarUtil;
import org.light.utils.StringUtil;
import org.light.utils.TableStringUtil;
import org.light.utils.WriteableUtil;

public class GetPasswordUser extends Verb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("getPassword"+StringUtil.capFirst(this.domain.getStandardName()));		
			method.setNoContainer(true);
			List<Writeable> list = new ArrayList<Writeable>();
			list.add(new Statement(100L, 1, "<select id=\"" + StringUtil.lowerFirst(this.getVerbName())
			+ "\" parameterType=\"string\" resultType=\"string\" >"));
			list.add(new Statement(200L, 2, "select "+domain.findFieldByFixedName("password").getFeildNameAsTableColumn()+ " from "+ domain.getDbPrefix() +TableStringUtil.domainNametoTableName(domain) +" where "+domain.findFieldByFixedName("userName").getFeildNameAsTableColumn()+" = #{"+domain.findFieldByFixedName("userName").getLowerFirstFieldName()+"}"));
			list.add(new Statement(300L, 1, "</select>"));
			method.setMethodStatementList(WriteableUtil.merge(list));
			return method;
		}
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("getPassword"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("String"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());			
			method.addSignature(new Signature(1, this.domain.findFieldByFixedName("userName").getLowerFirstFieldName(), this.domain.findFieldByFixedName("userName").getFieldRawType(),this.domain.getType().getPackageToken()));
			return method;
		}
	}

	@Override
	public String generateDaoImplMethodString() throws Exception {
		if (this.denied) return null;
		else {
			return generateDaoImplMethod().generateMethodString();
		}
	}

	@Override
	public String generateDaoMethodDefinitionString() throws Exception {
		if (this.denied) return null;
		else {
			return generateDaoMethodDefinition().generateMethodDefinition();
		}
	}

	@Override
	public String generateDaoImplMethodStringWithSerial() throws Exception {
		if (this.denied) return null;
		else {
			Method m = this.generateDaoImplMethod();
			m.setContent(m.generateMethodContentStringWithSerial());
			m.setMethodStatementList(null);
			return m.generateMethodString();
		}
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("getPassword"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("String"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1, this.domain.findFieldByFixedName("userName").getLowerFirstFieldName(), this.domain.findFieldByFixedName("userName").getFieldRawType(),this.domain.getType().getPackageToken()));
			return method;
		}
	}

	@Override
	public String generateServiceMethodDefinitionString() throws Exception {
		if (this.denied) return null;
		else {
			return generateServiceMethodDefinition().generateMethodDefinition();
		}
	}

	@Override
	public Method generateServiceImplMethod() throws Exception  {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("getPassword"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("String"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport("org.springframework.transaction.annotation.Transactional");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoSuffix()+"."+this.domain.getStandardName()+"Dao");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			method.addMetaData("Override");		
			
			method.addSignature(new Signature(1, this.domain.findFieldByFixedName("userName").getLowerFirstFieldName(), this.domain.findFieldByFixedName("userName").getFieldRawType(),this.domain.getType().getPackageToken()));
			List<Writeable> list = new ArrayList<Writeable>();
			list.add(new Statement(1000L,2, "return "+InterVarUtil.DB.dao.getVarName()+ ".getPassword"+domain.getStandardName()+"("+domain.findFieldByFixedName("userName").getLowerFirstFieldName()+");"));
			method.setMethodStatementList(WriteableUtil.merge(list));
			return method;
		}
	}

	@Override
	public String generateServiceImplMethodString() throws Exception {
		if (this.denied) return null;
		else {
			return generateServiceImplMethod().generateMethodString();
		}
	}

	@Override
	public String generateServiceImplMethodStringWithSerial() throws Exception  {
		if (this.denied) return null;
		else {
			Method m = this.generateServiceImplMethod();
			m.setContent(m.generateMethodContentStringWithSerial());
			m.setMethodStatementList(null);
			return m.generateMethodString();
		}
	}
	
	public GetPasswordUser(){
		super();
		this.setLabel("获取用户密码");
	}
	
	public GetPasswordUser(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("GetPasswordUser");
		this.setVerbName("GetPassword"+StringUtil.capFirst(this.domain.getStandardName()));
		this.setLabel("获取用户密码");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("GetPasswordUser");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("getPassword"+this.domain.getCapFirstDomainName());
			method.setReturnType(new Type("Map<String,Object>"));
			method.setThrowException(true);
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			method.addSignature(new Signature(1,this.domain.findFieldByFixedName("userName").getLowerFirstFieldName(),"String"));
			method.addMetaData("RequestMapping(value = \"/getPassword"+this.domain.getCapFirstDomainName()+"\", method = RequestMethod.POST)");
	
			List<Writeable> wlist = new ArrayList<Writeable>();
			Var service = new Var("service", new Type(this.domain.getStandardName()+"Service",this.domain.getPackageToken()));
			Var resultMap = new Var("result", new Type("TreeMap<String,Object>","java.util"));
			wlist.add(NamedStatementGenerator.getJsonResultMap(1000L, 2, resultMap));
			wlist.add(new Statement(1500L,2,this.domain.getCapFirstDomainNameWithSuffix() + " "+this.domain.getLowerFirstDomainName() + " = service.find" +this.domain.getCapFirstDomainName()+"By"+this.domain.getDomainName().getCapFirstFieldName()+"("+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+");"));
					
			long serial = 2000L;
			boolean imageExists = false;
			for (Field f:this.domain.getPlainFields()) {
				if (f.getFieldType().equalsIgnoreCase("image")) {
					imageExists = true;
					wlist.add(new Statement(serial,2,"if (temp"+this.domain.getCapFirstDomainName()+"!=null && temp"+this.domain.getCapFirstDomainName()+".get"+f.getCapFirstFieldName()+"()!=null) {"));
					wlist.add(new Statement(serial+1000L,3,this.domain.getLowerFirstDomainName()+".set"+f.getCapFirstFieldName()+"(temp"+this.domain.getCapFirstDomainName()+".get"+f.getCapFirstFieldName()+"());"));
					wlist.add(new Statement(serial+2000L,2,"}"));
					serial += 3000L;
				}				
			}
			if (imageExists) 
			{
				wlist.add(new Statement(serial+1000L,2,"temp"+this.domain.getCapFirstDomainName()+" = new  "+this.domain.getCapFirstDomainNameWithSuffix()+"();"));
				serial += 2000L;
			}			
			wlist.add(NamedStatementGenerator.getCallServiceMethod(serial, 2, service, generateServiceMethodDefinition()));
			wlist.add(NamedStatementListGenerator.getPutJsonResultMapWithSuccessAndNull(serial+1000L, 2, resultMap));
			wlist.add(new Statement(serial+2000L, 2, "return " + resultMap.getVarName()+";"));			
			method.setMethodStatementList(WriteableUtil.merge(wlist));		
			return method;
		}
	}
	

	@Override
	public String generateControllerMethodString()throws Exception {
		if (this.denied) return null;
		else {
			Method m = this.generateControllerMethod();
			return m.generateMethodString();
		}
	}



	@Override
	public String generateControllerMethodStringWithSerial() throws Exception {
		if (this.denied) return null;
		else {
			Method m = this.generateControllerMethod();
			m.setContent(m.generateMethodContentStringWithSerial());
			m.setMethodStatementList(null);
			return m.generateMethodString();
		}
	}



	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		if (this.denied) return null;
		else {
			JavascriptBlock block = new JavascriptBlock();
			block.setSerial(100);
			block.setStandardName("getPassword"+domain.getCapFirstDomainName());
			StatementList sl = new StatementList();
			sl.add(new Statement(1000,0, "{"));
			if (this.domain.getLanguage().equalsIgnoreCase("english")) {
				sl.add(new Statement(2000, 1, "text:'Add',"));
			}else {
				sl.add(new Statement(2000,1, "text:'新增',"));
			}
			sl.add(new Statement(3000,1, "iconCls:'icon-add',"));
			sl.add(new Statement(4000,1, "handler:function(){"));
			sl.add(new Statement(5000,2, "$('#wadd"+this.domain.getCapFirstDomainName()+"').window('open');"));
			sl.add(new Statement(6000,1, "}"));
			sl.add(new Statement(7000,0, "}"));
			block.setMethodStatementList(sl);
			return block;			
		}
	}



	@Override
	public String generateEasyUIJSButtonBlockString() throws Exception {
		if (this.denied) return null;
		else {
			return generateEasyUIJSButtonBlock().generateBlockContentString();
		}
	}



	@Override
	public String generateEasyUIJSButtonBlockStringWithSerial() throws Exception {
		if (this.denied) return null;
		else {
			return generateEasyUIJSButtonBlock().generateBlockContentStringWithSerial();
		}
	}



	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		if (this.denied) return null;
		else {
			Domain domain = this.domain;
			JavascriptMethod method = new JavascriptMethod();
			method.setSerial(200);
			method.setStandardName("getPassword"+domain.getCapFirstDomainName());
			
			StatementList sl = new StatementList();
			sl.add(new Statement(1000,1, "if ($(\"#ff\").form(\"validate\")) {"));
			sl.add(new Statement(2000,2, "$.ajax({"));
			sl.add(new Statement(3000,3, "type: \"post\","));
			sl.add(new Statement(4000,3, "url: \"../"+this.domain.getControllerPackagePrefix()+domain.getLowerFirstDomainName()+domain.getControllerNamingSuffix()+"/add"+domain.getCapFirstDomainName()+"\","));
			sl.add(new Statement(5000,3, "data: JSON.stringify({"));
			long serial = 6000;
			for (Field f: domain.getFieldsWithoutId()){
				if (f instanceof Dropdown){
					sl.add(new Statement(serial,4, f.getLowerFirstFieldName()+":$(\"#ff\").find(\"#"+f.getLowerFirstFieldName()+"\").combobox(\"getValue\"),"));
				} else if (f.getFieldType().equalsIgnoreCase("boolean")){
					sl.add(new Statement(serial,4, f.getLowerFirstFieldName()+":parseBoolean($(\"#ff\").find(\"input[name='"+f.getLowerFirstFieldName()+"']:checked\").val()),"));								
				} else {
					sl.add(new Statement(serial,4, f.getLowerFirstFieldName()+":$(\"#ff\").find(\"#"+f.getLowerFirstFieldName()+"\").val(),"));
				}
				serial+=1000;
			}				
			sl.add(new Statement(serial,3, "}),"));
			sl.add(new Statement(serial+1000,3, "dataType: 'json',"));
			sl.add(new Statement(serial+2000,3, "contentType:\"application/json;charset=UTF-8\","));
			sl.add(new Statement(serial+3000,3, "success: function(data, textStatus) {"));
			sl.add(new Statement(serial+4000,4, "if (data.success) {"));
			sl.add(new Statement(serial+5000,5, "$('#ff').form('clear');"));
			sl.add(new Statement(serial+6000,5, "$(\"#ff\").find(\"input[name='"+domain.getActive().getLowerFirstFieldName()+"']\").get(0).checked = true;"));
			sl.add(new Statement(serial+7000,5, "$(\"#wadd"+domain.getCapFirstDomainName()+"\").window('close');"));
			sl.add(new Statement(serial+8000,5, "$(\"#dg\").datagrid(\"load\");"));
			sl.add(new Statement(serial+9000,4, "}"));
			sl.add(new Statement(serial+10000,4, "},"));
			sl.add(new Statement(serial+11000,3, "complete : function(XMLHttpRequest, textStatus) {"));
			sl.add(new Statement(serial+12000,3, "},"));
			sl.add(new Statement(serial+13000,3, "error : function(XMLHttpRequest,textStatus,errorThrown) {"));
			sl.add(new Statement(serial+14000,4, "alert(\"Error:\"+textStatus);"));
			sl.add(new Statement(serial+15000,4, "alert(errorThrown.toString());"));
			sl.add(new Statement(serial+16000,3, "}"));
			sl.add(new Statement(serial+17000,2, "});"));
			sl.add(new Statement(serial+18000,1, "}"));
			
			method.setMethodStatementList(sl);
			return method;	
		}
	}



	@Override
	public String generateEasyUIJSActionString() throws Exception {
		if (this.denied) return null;
		else {
			return generateEasyUIJSActionMethod().generateMethodString();
		}
	}



	@Override
	public String generateEasyUIJSActionStringWithSerial() throws Exception {
		if (this.denied) return null;
		else {
			return generateEasyUIJSActionMethod().generateMethodStringWithSerial();
		}
	}

}
